package com.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.order.bean.Order;
import com.order.feign.ProductFeignClient;
import com.order.feign.WeatherFeignClient;
import com.order.service.OrderService;
import com.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    WeatherFeignClient  weatherFeignClient;
    @Override
    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    public Order createOrder(Long productId, Long userId) {
//        Product product = getProductFromRemoteWithAnnotation(productId);
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        // 总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("address");
        // 商品列表
        order.setProductList(List.of(product));

        return order;
    }

    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息"+e.getClass());

        return order;
    }

    private Product getProductFromRemote(Long productId){
        //1.获取到商品服务所在的所有机器ip+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        //远程url
        String url = "http://"+instance.getHost() + ":" + instance.getPort()+"/products/"+productId;
        log.info("远程请求:{}",url);
        //2. 给远程发送请求
        return restTemplate.getForObject(url, Product.class);
    }

    // 使用loadBalancerClient的负载均衡
    private Product getProductFromRemoteWithLoadBalancer(Long productId){
        //1.获取到商品服务所在的所有机器ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        //远程url
        String url = "http://"+choose.getHost() + ":" + choose.getPort()+"/products/"+productId;
        log.info("远程请求:{}",url);
        //2. 给远程发送请求
        return restTemplate.getForObject(url, Product.class);
    }

    //基于注解的负载均衡
    private Product getProductFromRemoteWithAnnotation(Long productId){
        String url = "http://service-product/products/"+productId;
        //2. 给远程发送请求,service-product会被动态替换
        return restTemplate.getForObject(url, Product.class);
    }
}
